<?php
class modmulticontent extends CI_Model{
	var $multicontent = "multi_content";

	function __construct()
	{
		parent::__construct();
	}
	
	function setData($id_multi_content,$id_menu,$title_multi_content,$date_multi_content,$enable)
	{
		$this->id_multi_content= $id_multi_content;
		$this->id_menu= $id_menu;
		$this->title_multi_content= $title_multi_content;
		$this->date_multi_content= $date_multi_content;
		$this->enable= $enable;
	}
	
	function getList($page,$uri_segment){
		$sql="
			SELECT c.*,sm.nama_menu,jc.jml_content
			FROM multi_content c
			left join setting_menu sm on sm.id_menu = c.id_menu
			left join(
			  select id_multi_content,count(*) as jml_content
			  from multi_container
			  group by id_multi_content
			)jc on c.id_multi_content = jc.id_multi_content
			limit ".$uri_segment.", ".$page."";
		$query=$this->db->query($sql);
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$result[] = $row;
			}
			return $result;
		} else {
			return false;
		}	
	}
	
	function create()
	{		
		$arrayData = array(
			'id_multi_content'=>$this->id_multi_content,
			'id_menu'=>$this->id_menu,
			'title_multi_content'=>$this->title_multi_content,
			'date_multi_content'=>$this->date_multi_content,
			'enable'=>$this->enable
		);
		return $this->db->insert($this->multicontent, $arrayData);
	}
	
	function update($id_multi_content)
	{
		$arrayData = array(
			'id_menu'=>$this->id_menu,
			'title_multi_content'=>$this->title_multi_content,
			'date_multi_content'=>$this->date_multi_content,
			'enable'=>$this->enable
		);
		$this->db->where('id_multi_content', $id_multi_content);
		return $this->db->update($this->multicontent, $arrayData);
	}
        
	function remove($id_multi_content)
	{
		$this->db->where('id_multi_content', $id_multi_content);
		return $this->db->delete($this->multicontent);
	}	
	
	function detail($id_multi_content)
	{
		$this->db->where('id_multi_content', $id_multi_content);
		$query = $this->db->get($this->multicontent);	
		return $query->result_array();
	}
	
	function getListSearch($name){
		$sql="
			SELECT c.*,sm.nama_menu,jc.jml_content
			FROM multi_content c
			left join setting_menu sm on sm.id_menu = c.id_menu
			left join(
			  select id_multi_content,count(*) as jml_content
			  from multi_container
			  group by id_multi_content
			)jc on c.id_multi_content = jc.id_multi_content
			
			where title_multi_content like '%".$name."%'";
		$query=$this->db->query($sql);
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$result[] = $row;
			}
			return $result;
		} else {
			return false;
		}	
	}
	
	function getComboContent(){
		$query = $this->db->get($this->multicontent);
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$result[] = $row;
			}
			return $result;
		} else {
			return false;
		}	
	}
	
	function getMultiContent($id_menu)
	{
		$this->db->where('id_menu', $id_menu);
		$query = $this->db->get($this->multicontent);	
		return $query->result_array();
	}
	
	function getBeritaLimitContent($id_menu)
	{
		$sql="
			SELECT m.id_menu, mc.id_multi_container, mc.title_multi_container, mc.container_multi_content, mc.file_upload, DATE_FORMAT( mc.date_multi_container,  '%d-%M-%Y' ) AS date_news
			FROM multi_content m
			LEFT JOIN multi_container mc ON m.id_multi_content = mc.id_multi_content
			WHERE m.id_menu =  ".$id_menu."
			AND m.enable =1
			AND mc.date_multi_container < SYSDATE() 
			ORDER BY mc.date_multi_container DESC
			LIMIT 3
		";
		$query=$this->db->query($sql);
		return $query->result_array();
	}
	
	function getListBeritaContent($id_menu,$page,$uri_segment)
	{
		$sql="
			SELECT  m.id_multi_content,m.id_menu, m.title_multi_content,
					mc.id_multi_container,mc.title_multi_container,
					mc.container_multi_content, mc.file_upload,DATE_FORMAT( mc.date_multi_container,  '%d-%M-%Y' ) as date_news
			FROM multi_content m
			LEFT JOIN multi_container mc on m.id_multi_content = mc.id_multi_content
			WHERE m.id_menu = ".$id_menu." and m.enable = 1
			ORDER BY mc.date_multi_container DESC 
			limit ".$uri_segment.", ".$page."";
		$query=$this->db->query($sql);
		return $query->result_array();
	}
}
?>